package com.xdcplus.web.controller;

import com.xdcplus.exceptions.domain.ResponseVO;
import com.xdcplus.mom.controller.AbstractController;

import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.bo.OpeJobTaskAlarmCodeSettingBO;
import com.xdcplus.web.common.pojo.dto.OpeJobTaskRecordDTO;
import com.xdcplus.web.common.pojo.dto.OpeJobTaskRecordFilterDTO;
import com.xdcplus.web.common.pojo.dto.task.JobTaskEapDTO;
import com.xdcplus.web.common.pojo.vo.OpeJobTaskRecordVO;
import com.xdcplus.web.service.OpeJobTaskRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Validation;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;


/**
 * (OpeJobTaskRecord)表服务控制层
 *
 * @author Zhecheng.Ren
 * @since 2024-10-30 14:52:12
 */
@Api(tags = "(OpeJobTaskRecord)") 
@Slf4j
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("opeJobTaskRecord")
public class OpeJobTaskRecordController extends AbstractController {

    @Autowired
    private OpeJobTaskRecordService opeJobTaskRecordService;

    @ApiOperation("新增")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO saveOpeJobTaskRecord(@RequestBody OpeJobTaskRecordDTO opeJobTaskRecordDTO) {

        log.info("saveOpeJobTaskRecord {}", opeJobTaskRecordDTO.toString());

        opeJobTaskRecordDTO.setCreatedUser(getAccount());
        opeJobTaskRecordService.saveOpeJobTaskRecord(opeJobTaskRecordDTO);

        return ResponseVO.success();
    }

    @ApiOperation("修改")
    @PutMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO updateOpeJobTaskRecord(@RequestBody OpeJobTaskRecordDTO opeJobTaskRecordDTO) {

        log.info("updateOpeJobTaskRecord {}", opeJobTaskRecordDTO.toString());

        opeJobTaskRecordDTO.setUpdatedUser(getAccount());
        opeJobTaskRecordService.updateOpeJobTaskRecord(opeJobTaskRecordDTO);

        return ResponseVO.success();
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/{opeJobTaskRecordId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "opeJobTaskRecordId", dataTypeClass = Long.class, value = "ID", required = true),
    })
    public ResponseVO deleteOpeJobTaskRecordLogical(@PathVariable("opeJobTaskRecordId")
                                         @NotNull(message = "ID不能为空") Long opeJobTaskRecordId) {

        log.info("deleteOpeJobTaskRecordLogical {}", opeJobTaskRecordId);

        opeJobTaskRecordService.deleteOpeJobTaskRecordLogical(opeJobTaskRecordId);

        return ResponseVO.success();
    }
    
    @ApiOperation("批量删除")
    @DeleteMapping(value = "/deleteByIds/{opeJobTaskRecordIds}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "opeJobTaskRecordId", dataTypeClass = Long.class, value = "ID", required = true),
    })
    public ResponseVO deleteOpeJobTaskRecordLogical(@PathVariable("opeJobTaskRecordIds")
                                         @NotNull(message = "ID不能为空") String opeJobTaskRecordIds) {

        log.info("deleteOpeJobTaskRecordByIds {}", opeJobTaskRecordIds);

        opeJobTaskRecordService.deleteOpeJobTaskRecordByIds(opeJobTaskRecordIds);

        return ResponseVO.success();
    }

    @ApiOperation("查询")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<PageVO<OpeJobTaskRecordVO>> findOpeJobTaskRecord(OpeJobTaskRecordFilterDTO opeJobTaskRecordFilterDTO) {

        log.info("findOpeJobTaskRecord {}", opeJobTaskRecordFilterDTO);

        Validation.buildDefaultValidatorFactory().getValidator().validate(opeJobTaskRecordFilterDTO);

        return ResponseVO.success(opeJobTaskRecordService.queryOpeJobTaskRecord(opeJobTaskRecordFilterDTO));
    }

    @ApiOperation("根据设备查询报警代码配置")
    @GetMapping(value = "/alarmCode/{toolCode}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "toolCode", dataTypeClass = String.class, value = "toolCode", required = true),
    })
    public ResponseVO<List<OpeJobTaskAlarmCodeSettingBO>> queryAlarmSettingByToolCode(@PathVariable("toolCode")
                                                                                          @NotNull(message = "设备号不能为空") String toolCode) {

        log.info("queryAlarmSettingByToolCode {}", toolCode);

        return ResponseVO.success(opeJobTaskRecordService.queryOpeJobTaskAlarmCodeSetting(toolCode));
    }

    @ApiOperation("接收eap报警")
    @PostMapping(value = "sendAlarm", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO saveEapAlarm(@RequestBody JobTaskEapDTO jobTaskEapDTO) {

        log.info("saveEapAlarm {}", jobTaskEapDTO.toString());

        opeJobTaskRecordService.saveEapAlarm(jobTaskEapDTO);

        return ResponseVO.success();
    }

    @ApiOperation("信息导出")
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<Void> exportExcel(@RequestBody OpeJobTaskRecordFilterDTO opeJobTaskRecordFilterDTO, HttpServletResponse response) throws IOException {
        opeJobTaskRecordService.exportData(opeJobTaskRecordFilterDTO, response);
        return ResponseVO.success();
    }


}
